package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.orderVo.UcenterMemberOrderVo;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-23
 */
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin
public class UcenterMemberController {
    @Autowired
    UcenterMemberService ucenterMemberService;

    //登录
    @PostMapping("login")
    public R login(@RequestBody UcenterMember member){
        String token=ucenterMemberService.login(member);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("register")
    public R register(@RequestBody RegisterVo vo){
        ucenterMemberService.register(vo);
        return R.ok();
    }

    /**
     * 根据token获取用户信息
     *  接收一个HttpServletRequest对象，该对象存放token值，通过Jwt工具类就可以获取到用户id
     * @param request
     * @return
     */
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = ucenterMemberService.getById(memberId);
        return R.ok().data("memberInfo",member);
    }

    /**
     * 根据会员id获取会员信息
     * 生成订单时需要
     * @param memberId
     * @return
     */
    @PostMapping("/getMemberInfoOrder/{memberId}")
    public UcenterMemberOrderVo getMemberInfoOrder(@PathVariable("memberId") String memberId){
        UcenterMember ucenterMember = ucenterMemberService.getById(memberId);
        UcenterMemberOrderVo vo=new UcenterMemberOrderVo();
        BeanUtils.copyProperties(ucenterMember,vo);
        return vo;
    }

    //根据时间统计用户注册数据
    @GetMapping("/countRegister/{day}")
    public R countRegister(@PathVariable("day") String day){
        Integer count=ucenterMemberService.countRegister(day);
        return R.ok().data("register",count);
    }

}

